package it.uniparthenope.cm.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.angryelectron.thingspeak.Channel;
import com.angryelectron.thingspeak.Entry;
import com.angryelectron.thingspeak.Feed;
import com.angryelectron.thingspeak.FeedParameters;
import com.angryelectron.thingspeak.ThingSpeakException;
import com.mashape.unirest.http.exceptions.UnirestException;

@ManagedBean
public class ViewFeedsManagedBean {
	private List<Entry> feeds= new ArrayList<Entry>() ;
	private int numFeeds=10;
	public ViewFeedsManagedBean(){
		
	}
	
	@PostConstruct
	public void populateEntryList(){
		refreshList();
	}

	
	public void refreshList(){
		String apiWriteKey = "your-channel-write-key";
        Channel channel = new Channel(25485);
       // Channel channel = new Channel(1);
      //  Entry entry = new Entry();
       // entry.setField(1, "hello");
       // channel.update(entry);

       // Channel channel = new Channel(1234);
        //Entry entry;
        try {
            //entry = channel.getLastChannelEntry();
            FeedParameters fp=new FeedParameters();
            fp.results(numFeeds);
            Feed f=channel.getChannelFeed(fp);
            feeds=f.getEntryList();
        } catch (ThingSpeakException ex) {
            Logger.getLogger(ViewFeedsManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnirestException ex) {
            Logger.getLogger(ViewFeedsManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
		
	}
	public int getNumFeeds() {
		return numFeeds;
	}

	public void setNumFeeds(int numFeeds) {
		this.numFeeds = numFeeds;
		refreshList();
	}

	public List<Entry> getFeeds() {
		return feeds;
	}

	public void setFeeds(List<Entry> feeds) {
		this.feeds = feeds;
	}
	
	
}
